package comunicating.classes;

import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

import noncomunicating.classes.*;

public class Receiver extends abstracts.Step{
	
	/*
	 * The receiver class gets the HTTP requests from the socket and places them into the request object
	 */
	public Receiver(Request request){
		super(request);
		
		Socket clientSocket = request.getClientSocket();
		Scanner input;
		try {
			input = new Scanner(clientSocket.getInputStream());
			input.useDelimiter("\r\n\r\n"); //This is set this way so that I can retrieve the entire HTTP request with headers as one string. 
			String inputString;
			try {
				inputString = input.next();
				request.setHttpRequest(inputString); //return the HTTP  request to the request object
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public void printOutput(boolean verbose) {
		System.out.println(this.getOutput(verbose));
	}

	@Override
	public String getOutput(boolean verbose) {
		String retString = null;
		if(verbose){
			retString += (request.getName() + ": HTTP Recieved. Request is:\n");
			retString += request.getHttpRequest() + "\n";
		}
		else{
			retString = "HTTP Recieved.\n";
		}
		return retString;
	}
	
	
}